import { prisma } from '@/lib/prisma';
import { getServerSession } from 'next-auth';
import { notFound, redirect } from 'next/navigation';
import { ShareSettings } from './ShareSettings';
import { PdfExport } from './PdfExport';

async function getTrip(id: string) {
  const trip = await prisma.trip.findUnique({
    where: { id },
    include: {
      activities: {
        include: { destination: true },
        orderBy: { date: 'asc' },
      },
      user: {
        select: { name: true, username: true },
      },
    },
  });

  if (!trip) notFound();
  return trip;
}

export default async function SharePage({ params }: { params: { id: string } }) {
  const session = await getServerSession();
  if (!session?.user) {
    redirect('/login');
  }

  const trip = await getTrip(params.id);
  
  // 验证权限
  if (trip.userId !== session.user.id) {
    notFound();
  }

  return (
    <div className="max-w-4xl mx-auto py-8 px-4">
      <h1 className="text-2xl font-bold mb-6">分享行程</h1>
      <ShareSettings trip={trip} />
      
      <div className="mt-8">
        <h2 className="text-lg font-semibold mb-4">导出选项</h2>
        <div className="space-y-4">
          <button
            onClick={() => window.print()}
            className="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50"
          >
            打印行程
          </button>
          <PdfExport trip={trip} />
        </div>
      </div>
    </div>
  );
} 